package com.qianxx.view.wheel.hh;

import android.content.Context;
import android.widget.LinearLayout;

import com.qianxx.base.R;
import com.qianxx.utils.DisplayUtil;
import com.qianxx.view.dialog.ExSweetAlertDialog;
import com.qianxx.view.wheel.adapter.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created on 2016/12/14 下午3:11.
 *
 * @author binwin20
 */

public class SelectorDialog extends ExSweetAlertDialog {

    private final WheelConfig mWheelConfig;
    private final SelectorListener mListener;
    private List<WheelView> mWheelViews;
    private int[] selectPosition;

    public SelectorDialog(Context context,
                          WheelConfig wheelConfig,
                          SelectorListener listener) {
        super(context, R.layout.dialog_selector);

        mWheelConfig = wheelConfig;
        mListener = listener;
        selectPosition = new int[wheelConfig.getColumns()];

        initDialog();
        initWheel();
    }

    public void setWheel(int column, List<String> data, int defSelect) {
        WheelView wheelView = mWheelViews.get(column);
        String[] items = data.toArray(new String[data.size()]);
        ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(getContext(), items);
        wheelView.setViewAdapter(adapter);
        wheelView.setCurrentItem(defSelect);
    }

    public void setWheel(int column, List<String> data) {
        WheelView wheelView = mWheelViews.get(column);
        if (wheelView.getCurrentItem() >= data.size() - 1) {
            int index = Math.max(data.size() - 1, 0);
            wheelView.setCurrentItem(index);
        }
        String[] items = data.toArray(new String[data.size()]);
        ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(getContext(), items);
        wheelView.setViewAdapter(adapter);
    }

    private void initDialog() {
        setCancelable1(true);
        setCanceledOnTouchOutside1(true);
        setAnimIn(R.anim.dialog_selecter_in);
        setAnimOut(R.anim.dialog_selecter_out);
        setWidth(DisplayUtil.getScreenW(getContext()));
        setHeight(DisplayUtil.getScreenH(getContext()) - DisplayUtil.getStatusHeight(getContext()));
        setText(R.id.dialog_title, mWheelConfig.getTitle());
        setListener(R.id.dialog_cancel_button, dialog -> {
            dialog.dismissWithAnimation();
            if (mListener != null) mListener.onCancel();
        });
        setListener(R.id.dialog_confirm_button, dialog -> {
            dialog.dismissWithAnimation();
            if (mListener != null) mListener.onConfirm(selectPosition);
        });
        setListener(R.id.space, dialog -> {
            dialog.dismissWithAnimation();
            if (mListener != null) mListener.onCancel();
        });
    }

    private void initWheel() {
        LinearLayout container = (LinearLayout) getViewById(R.id.wheel_container);
        mWheelViews = new ArrayList<>();
        for (int i = 0; i < mWheelConfig.getColumns(); i++) {
            WheelView view = new WheelView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    0, LinearLayout.LayoutParams.WRAP_CONTENT, mWheelConfig.getWeights()[i]);
            view.setLayoutParams(params);
            container.addView(view);
            mWheelViews.add(view);
            final int currentColumn = i;
            view.addChangingListener((wheel, oldValue, newValue) -> {
                if (mListener != null) {
                    mListener.onSelected(currentColumn, newValue, SelectorDialog.this);
                }
                selectPosition[currentColumn] = newValue;
            });
        }
    }

    public static class WheelConfig {

        private final int mColumns;
        private final String mTitle;
        private int[] mWeights;

        private WheelConfig(int columns, String title, int[] weights) {
            mColumns = columns;
            mTitle = title;
            mWeights = weights;
        }

        /**
         * 滚轮列数
         *
         * @return
         */
        public int getColumns() {
            return mColumns;
        }

        /**
         * 滚轮标题
         *
         * @return
         */
        public String getTitle() {
            return mTitle;
        }

        /**
         * 滚轮宽度比
         *
         * @return
         */
        public int[] getWeights() {
            return mWeights;
        }

        public static class Builder {
            private int mColumns;
            private String mTitle;
            private int[] mWeights;

            public Builder(int columns, String title) {
                mColumns = columns;
                mTitle = title;
            }

            public WheelConfig build() {
                if (mWeights == null) {
                    mWeights = new int[mColumns];
                    for (int i = 0; i < mWeights.length; ++i) {
                        mWeights[i] = 1;
                    }
                }
                return new WheelConfig(mColumns, mTitle, mWeights);
            }

            /**
             * 如果不设置这项, 所有滚轮一样大
             *
             * @param weights - 长度必须是 columns
             * @return
             */
            public Builder setWeights(int[] weights) {
                mWeights = weights;
                if (mWeights.length != mColumns) {
                    new Error("weights 长度必须是 columns");
                }
                return this;
            }
        }
    }

    public interface ISelectorDialog {
        /**
         * 设置滚轮显示
         *
         * @param column    第 column 列
         * @param list      显示数据
         * @param defSelect 默认选择位置
         */
        void setWheel(int column, List<String> list, int defSelect);

        /**
         * 设置滚轮显示
         *
         * @param column 第 column 列
         * @param list   显示数据
         */
        void setWheel(int column, List<String> list);
    }

    public interface SelectorListener {
        /**
         * 滚轮滚动之后
         *
         * @param column         滚动的列
         * @param selectPosition 位置
         */
        void onSelected(int column, int selectPosition, SelectorDialog dialog);

        /**
         * 点击取消
         */
        void onCancel();

        /**
         * 点击确认
         *
         * @param selectPositions 选择的位置
         */
        void onConfirm(int[] selectPositions);
    }
}
